<template>
  <div class="box-padding"
       ref="echartsWrapper">
    <Row :gutter="12">
      <Col span="4"
           offset="16"  v-if="this.personType === 1">
      <Select v-model="formData.enterpriseId" style="width: 90%;font-size: 11px;" ref="treeSelect" placeholder="机构">
      							<Option v-for="item in list1" :value="item.enterpriseId" :key="item.enterpriseId" style="display: none;">{{ item.title }}</Option>
      							<Tree :data="orgList" style="padding:0 10px" @on-select-change="checkEng"></Tree>
      						</Select>
      </Col>

      <Col span="4"  v-if="this.personType === 1">
        <Select v-if="this.personType != 3" clearable v-model="formData.projectTeamId" style="width: 90%;font-size: 11px;" placeholder="全部项目" @on-change="proChange">
          <Option v-for="item in proList" :value="item.id" :key="item.id">{{ item.shortName }}</Option>
        </Select>
        <Select v-if="this.personType == 3" :clearable="(proList.length>1)" v-model="formData.projectTeamId" style="width: 90%;font-size: 11px;" placeholder="全部项目" @on-change="proChange">
          <Option v-for="item in proList" :value="item.id" :key="item.id">{{ item.shortName }}</Option>
        </Select>
       </Col>
       <Col span="4"  v-if="this.personType !== 1" offset="19" >
           <Select v-if="this.personType != 3" clearable v-model="formData.projectTeamId" style="width: 90%;font-size: 11px;" placeholder="全部项目" @on-change="proChange">
             <Option v-for="item in proList" :value="item.id" :key="item.id">{{ item.shortName }}</Option>
           </Select>
           <Select v-if="this.personType == 3" :clearable="(proList.length>1)" v-model="formData.projectTeamId" style="width: 90%;font-size: 11px;" placeholder="全部项目" @on-change="proChange">
             <Option v-for="item in proList" :value="item.id" :key="item.id">{{ item.shortName }}</Option>
           </Select>
       </Col>
      <Col span="4" v-show='false'>
         <Select v-model="formData.time" @on-change="getTime">
                <Option v-for="item in timeOptions"
                        :value="item.id"
                        :key="item.id">{{ item.name }}</Option>
              </Select>
      </Col>
    </Row>
    <first-line :data="todoTotal" :data2="overdueTotal" :pie="undo" v-if="showPic"></first-line>
    <Table border
           :columns="columns"
           :data="data"
           style="margin: 20px 0;">
    </Table>

    <second-line :data1="line1" :data2="line2" :name="lineName" v-if="showPic2">
    </second-line>
  </div>
</template>
<script>
import firstLine from './first.vue'
import secondLine from './second.vue'

export default {
  name:'statistics-measures',
  components: {
    firstLine,
    secondLine,

  },
  data() {
    return {
      todoTotal: 0,
      overdueTotal: 0,
      undo: [],
      timeOptions: [
        {name: '开累', id: 1},
        {name: '本周', id: 2},
        {name: '上周', id: 3},
        {name: '本月', id: 4},
        {name: '上月', id: 5},
        {name: '本季度', id: 6},
        {name: '本年', id: 7},
      ],
      showPic: false,
      showPic2: false,
      showPic3: false,
      userDetail: JSON.parse(localStorage.getItem('userList')),
      personType: JSON.parse(localStorage.getItem('personType')),
      orgList: [], //机构树
      proList: [], //项目部树
      model1: '',
      list1: [],
      list2: [],
      monthList: [],
      data: [],
      formData: {
        time: 1
      },
      riskLevelData: [],
      measureSourceData: [],
      measureTypeData: [],
      thirdStatisticsFirst:[],
      thirdStatisticsSecond:[],
      lineName: [],
      line1: [],
      line2: [],

      columns: [
        {
          title: '序号',
          align: 'center',
          width: 70,
          render: (h, params) => {
            return h(
              'span',
              params.index + 1
            )
          }
        },
        {
          title: '系统待办事件',
          key: 'module',
          align: 'center'
        },
        {
          title: '合计',
          key: 'total',
          align: 'center'
        },
        {
          title: '红牌预警',
          key: 'redCount',
          align: 'center'
        },
        {
          title: '橙牌预警',
          key: 'orangeCount',
          align: 'center'
        },
        {
          title: '黄牌预警',
          key: 'yellowCount',
          align: 'center'
        },
        {
          title: '蓝牌预警',
          key: 'blueCount',
          align: 'center'
        }
      ]
    }
  },
  mounted() {
    //1集团 2工程公司 3项目部
    if(this.personType === 3) {

      this.formData.enterpriseId = this.userDetail.projectTeamId;
      this.getProTree({
        objectId: this.userDetail.projectTeamId
      })

    } else if(this.personType === 1) {
      this.getOrgTree();
    } else if(this.personType === 2) {

      this.formData.enterpriseId = this.userDetail.enterpriseId;
      this.getProTree({
        objectId: this.userDetail.enterpriseId
      })

    }

  },
  methods: {
        //机构树
  			getOrgTree() {
  				this.orgList = [];
  				this.list1 = [];
  				this.$api.measurePlan.getOrgTree().then((res) => {
  					if(res.code == 200) {
  						this.orgList = res.data;
  						this.checkEng(this.orgList);
  					} else {
  						this.$Message.error(res.data.msg);
  					}
  				})
  			},
  			//点击机构树
        checkEng(data) {
          if(data.length > 0) {
            this.formData.projectTeamId = '';
            this.list1 = []
            let title = data[0].title
            if(this.$route.query.orgId){
              let enterpriseId = this.$route.query.orgId
              this.list1.push({
                title,
                enterpriseId
              })
              this.getProTree(data[0]);
              this.formData.enterpriseId = enterpriseId
              this.$refs.treeSelect.visible = false

            }else if(data[0].objectId) {
              let enterpriseId = data[0].objectId
              this.list1.push({
                title,
                enterpriseId
              })
              this.getProTree(data[0]);
              this.formData.enterpriseId = enterpriseId
              this.$refs.treeSelect.visible = false

            } else {
              this.$Message.warning('请选择' + title + '下的子集')
            }
          }

        },
        //获取项目
        getProTree(data) {
          this.proList = [];
          this.formData.projectTeamId = '';
          this.$api.measurePlan.getProList(data.objectId).then(res => {
            if(res.code === 200) {
              if(res.data.length > 0) {
                res.data.forEach(item => {
                  this.proList.push(item);
                });
                if(this.proList.length == 1 && this.personType === 3) {
                  this.formData.projectTeamId = this.proList[0].id;
                }
                this.alertTodo()
                this.alertTable()
                this.alertTrend()
              }
            } else {
              this.$Message.error(res.message)
            }
          })
        },
        checkPro(data) { //点击项目树
            if(data.length > 0) {
              this.list2 = []
              let proId = data[0].id
            let title = data[0].shortName
              this.list2.push({
                proId,
                title
              })
              this.formData.projectId = proId
            this.$refs.treeSelectPro.visible = false
            }
          },
          alertTrend() { //预警趋势
              this.showPic2 = false
              let params = {
                "orgId": this.formData.enterpriseId,
                 "proId": this.formData.projectId || '',
                 "controlTime": this.formData.date
              }
              this.$api.alert.alertTrend(this.formData).then(result=> {
                if(result.code == 200) {
                  if(result.data && result.data.length > 0) {
                      this.lineName = []
                      this.line1 = []
                      this.line2 = []
                      result.data.forEach(item=>{
                        this.lineName.push(item.date)
                        this.line1.push(item.todoCount)
                        this.line2.push(item.overdueCount)

                      })
                  } else {
                      this.lineName = []
                       this.line1 = []
                       this.line2 = []
                  }
                  this.showPic2 = true
                } else {
                  this.lineName = []
                  this.line1 = []
                  this.line2 = []
                  this.showPic2 = true
                  this.$Message.warning(res.message)
                }
              }).catch(() => {
                  this.showPic2 = true
                  this.$Message.warning('请求图表失败')
              })
          },
          alertTodo() { //待办预警
              this.showPic = false
              this.$api.alert.alertTodo(this.formData).then(res => {
                if(res.code == 200) {
                  this.todoTotal = res.data.todoTotal
                  this.overdueTotal = res.data.overdueTotal
                  this.undo = [res.data.redCount, res.data.blueCount, res.data.yellowCount, res.data.orangeCount]
                  let sum = 0
                  this.undo.forEach(item=>{
                    sum += item
                  })
                  if(sum == 0) {
                    this.undo = []
                  }
                  this.showPic = true
                } else {
                    this.todoTotal = res.data.todoTotal
                    this.overdueTotal = res.data.overdueTotal
                    this.undo = []
                    this.showPic = true
                    this.$Message.error(res.message)
                }
              }).catch(() => {
                  this.showPic = true
                  this.$Message.warning('请求图表失败')
              })
          },
          alertTable() { //预警表格统计

            let params = {
              "orgId": this.formData.enterpriseId,
               "proId": this.formData.projectId || '',
               "controlTime": this.formData.date,

            }
            this.$api.alert.alertTable(this.formData).then(result=> {
              if(result.code == 200) {
                 if(result.data.length > 0) {
                    let obj = {
                      total: 0,
                      module: '合计',
                      blueCount: 0,
                      orangeCount: 0,
                      redCount: 0,
                      yellowCount: 0,
                    }
                    result.data.forEach(item =>{
                        obj.total += item.total
                        obj.blueCount += item.blueCount
                        obj.orangeCount += item.orangeCount
                        obj.redCount += item.redCount
                        obj.yellowCount += item.yellowCount
                    })
                    result.data.push(obj)
                 }
                 this.data = result.data
              } else {
                    this.data = []
                    this.$Message.error(res.message)
              }
            }).catch(() => {
                this.$Message.warning('请求预警失败')
            })
        },
        proChange() { //点击项目
          this.alertTrend()
          this.alertTable()
          this.alertTodo()
        },
        getTime(){
          this.alertTrend()
          this.alertTable()
          this.alertTodo()
        },
  }
}
</script>
<style scoped>
 .ivu-input-suffix {
    display: none !important
 }
</style>